/**
 * This file is part of Sonedyan.
 * 
 * Sonedyan is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation;
 * either version 3 of the License, or (at your option) any
 * later version.
 *
 * Sonedyan is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public
 * License along with Octave; see the file COPYING.  If not
 * see <http://www.gnu.org/licenses/>.
 * 
 * Copyright (C) 2009-2013 Jimmy Dubuisson <jimmy.dubuisson@gmail.com>
 */

package org.unige.mpej.eckmann.sonedyan.mr;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.KeyValueTextInputFormat;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapred.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import org.apache.log4j.Logger;

public class TrianglesCounter extends Configured  implements Tool
{
	private static Logger log = Logger.getLogger(org.unige.mpej.eckmann.sonedyan.mr.TrianglesCounter.class);
	
	/**
	 * mapper
	 */
	public static class Map extends MapReduceBase implements Mapper<Text, Text, Text, IntWritable>
	{
		private final static IntWritable one = new IntWritable(1);
		
		// Input: <u; (v, w)>
		@Override
		public void map(Text key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException
		{
			output.collect(key, one);
			
			String[] vs = value.toString().split(",");
			output.collect(new Text(vs[0]), one);
			output.collect(new Text(vs[1]), one);
		}
	}

	/**
	 * reducer
	 */
	public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable>
	{
		@Override
		public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException
		{
			int sum = 0;
			while (values.hasNext())
			{
			             sum += values.next().get();
			}
			output.collect(key, new IntWritable(sum));
		}
	}
	
	/** 
	 * Hadoop tutorial: http://developer.yahoo.com/hadoop/tutorial/
	 */
	public final int run(final String[] args) throws Exception
	{
		// http://blogs.msdn.com/b/avkashchauhan/archive/2012/03/29/how-to-chain-multiple-mapreduce-jobs-in-hadoop.aspx
		
		/*
		 * configuring job 1
		 */
		JobConf conf1 = new JobConf(getConf(), TrianglesCounter.class);
		
		conf1.setJobName("TC");
		
		conf1.setMapperClass(Map.class);
		conf1.setCombinerClass(Reduce.class);
		conf1.setReducerClass(Reduce.class);

		conf1.setInputFormat(KeyValueTextInputFormat.class);
		conf1.setOutputFormat(TextOutputFormat.class);
		
		conf1.setOutputKeyClass(Text.class);
		conf1.setOutputValueClass(IntWritable.class);

		String inputPath = args[0];
		FileInputFormat.setInputPaths(conf1, new Path(inputPath));
		FileOutputFormat.setOutputPath(conf1, new Path("triangles-distribution"));
		
		JobClient.runJob(conf1);
			
		return 0;
	}
	
	/**
	 * 
	 */
	public static void main(final String[] args) throws Exception
	{
		  Configuration conf = new Configuration();
		  
		  // it is possible to complete the job arguments here
		  // String[] args2 = new String[] { "-D", "mapred.reduce.tasks=3"};
		  int res = ToolRunner.run(conf, new org.unige.mpej.eckmann.sonedyan.mr.TrianglesCounter(), args);
		  System.exit(res);
	}
}
